package com.zp.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.bo.SysOrgDictBo;
import com.zp.system.api.vo.SysOrgDictVo;
import com.zp.system.service.ISysOrgDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 机构数据字典控制器
 * 前端访问路由地址为:/system/sysOrgDict
 *
 * @author zhang peng
 * @ date 2024-03-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysOrgDict")
public class SysOrgDictController extends BaseController {

    private final ISysOrgDictService iSysOrgDictService;

    /**
     * 查询机构数据字典列表 (分页)
     */
    @SaCheckPermission("system:sysOrgDict:list")
    @GetMapping("/selectPage")
    public TableDataInfo<SysOrgDictVo> selectPage(SysOrgDictBo bo, PageQuery pageQuery) {
        return iSysOrgDictService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询机构数据字典列表 (不分页)
     */
//    @SaCheckPermission("system:sysOrgDict:list")
    @GetMapping("/list")
    public R<List<SysOrgDictVo>> list(SysOrgDictBo bo) {
        List<SysOrgDictVo> list = iSysOrgDictService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出机构数据字典列表
     */
    @SaCheckPermission("system:sysOrgDict:export")
    @Log(title = "机构数据字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysOrgDictBo bo, HttpServletResponse response) {
        List<SysOrgDictVo> list = iSysOrgDictService.queryList(bo);
        ExcelUtil.exportExcel(list, "机构数据字典", SysOrgDictVo.class, response);
    }

    /**
     * 获取机构数据字典详细信息
     *
     * @param orgDictId 主键
     */
    @SaCheckPermission("system:sysOrgDict:list")
    @GetMapping("/{orgDictId}")
    public R<SysOrgDictVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String orgDictId) {
        return R.ok(iSysOrgDictService.queryById(orgDictId));
    }

    /**
     * 新增机构数据字典
     */
    @SaCheckPermission("system:sysOrgDict:add")
    @Log(title = "机构数据字典", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOrgDictBo bo) {
        return toAjax(iSysOrgDictService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 新增机构数据字典(批量插入)
     */
    @SaCheckPermission("system:sysOrgDict:add")
    @Log(title = "机构数据字典", businessType = BusinessType.INSERT)
    @PostMapping("/saveBatchOrgDictInData")
    public R<Void> saveBatchOrgDictInData(@Validated(AddGroup.class) @RequestBody SysOrgDictBo bo) {
        return toAjax(iSysOrgDictService.saveBatchOrgDictInData(bo) ? 1 : 0);
    }

    /**
     * 修改机构数据字典
     */
    @SaCheckPermission("system:sysOrgDict:edit")
    @Log(title = "机构数据字典", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOrgDictBo bo) {
        return toAjax(iSysOrgDictService.updateByBo(bo) ? 1 : 0);
    }


    /**
     * 添加对照
     */
    @Log(title = "机构数据字典", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSysOrgDictToClinic")
    public R<Void> updateSysOrgDictToClinic(@Validated(EditGroup.class) @RequestBody SysOrgDictBo bo) {
        return toAjax(iSysOrgDictService.updateSysOrgDictToClinic(bo) ? 1 : 0);
    }




    /**
     * 删除机构数据字典
     *
     * @param orgDictIds 主键串
     */
    @SaCheckPermission("system:sysOrgDict:remove")
    @Log(title = "机构数据字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orgDictIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] orgDictIds) {
        return toAjax(iSysOrgDictService.deleteWithValidByIds(Arrays.asList(orgDictIds), true) ? 1 : 0);
    }
}
